<?php

/**
 * Свалка ядерных функций
 */
class FDump implements iFDump {

	private function __construct () {}
	
	/**
	 * Подсчитывает количество вхождений определенного свойства объекта
	 * @param arrat $aTypes массив типов
	 * @param string $sFieldName название свойства
	 * @param boolean $sortByItems по-умолчанию сортировка по количеству вхождений, в противном случае по значениям свойства
	 * @param boolean $asc прямой (по-умолчанию) или обратный порядок сортировки
	 * @return array
	 */
	public static function getCountObjectValues (array $aTypes, $sFieldName, $sortByItems = false, $asc = true) {
		$db = DbController::getInstance ();
		
		$aSql = array ();
		
		foreach ($aTypes as $oType) {
		
			if (!($oType instanceof Type)) {
				exit ('1');
				return false;
			}
			
			if (!$oField = $oType->getFieldByName ($sFieldName)) {
				exit ('2');
				return false;
			}
			
			if (!$oField->isReal ()) {
				exit ('3');
				return false;
			}
			
			$character = $oField->getCharacter ();
					
			if ($character == 'tags') {
				$aSql[] = "SELECT `value`, COUNT(`value`) AS `items` FROM `explay_tags` WHERE `field_id` = " . $oField->getId() . " GROUP BY `value`"; // ORDER BY `" . ($sortByItems ? 'items' : 'value') . "` " . ($asc ? 'ASC' : 'DESC');
			}
			elseif ($character == 'guide' || $character == 'muliguide') {
				exit ('4');
				return false;
			}
			else {
				$aSql[] = "SELECT `" . $sFieldName . "` AS `value`, COUNT(`" . $sFieldName . "`) AS `items` FROM `" . $oType->getTableName() . "` GROUP BY `value`";
			}
			
		}
		
		if (count ($aSql) == 0) {
			exit ('5');
			return false;
		}
		
		$get = $db->query (implode (' UNION ', $aSql) . " ORDER BY `" . ($sortByItems ? 'items' : 'value') . "` " . ($asc ? 'ASC' : 'DESC'));
		
		$response = array (
			'values' => array ()
		);
		
		$summ = 0;
		$max = 0;
		$min = 0;
		
		while (list ($value, $items) = $db->fetch_row ($get)) {
			$items = (int) $items;
			
			$response['values'][] = array ('value' => $value, 'cnt' => $items);
			
			if ($items < $min || $min == 0) {
				$min = $items;
			}
			
			if ($items > $max) {
				$max = $items;
			}
			$summ += $items;
		}
		
		$response['min'] = $min;
		$response['max'] = $max;
		$response['summ'] = $summ;
		
		return $response;
	}
	
	/**
	 * @param integer $guideId
	 * @return array
	 */
	public function getGuideOptions ($guideId) {
		$objectsController = ObjectsController::getInstance ();
		$typesController = TypesController::getInstance ();
		
		if (!$oGuide = $objectsController->getObject ($typesController->getTypeByName ('explay_guide'), $guideId)) {
			throw new CoreException ('Guide not found');
		}
		
		$labelFieldName = FieldsController::getInstance()->getField($oGuide->getValue ('label_field_id'))->getName ();
		
		$sel = new Selection;
		$sel->addType ($typesController->getTypeById ($oGuide->getValue ('type_id')));
		
		$aObjects = $objectsController->getObjectsList ($sel->getList ());
		
		$response = array ();
		
		foreach ($aObjects as $object) {
			$response[] = array ($object->getId (), $object->getValue ($labelFieldName));
		}
		
		return $response;
	}

}